package com.lhz.project.system.controller.task;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.lhz.common.page.ResponseObject;
import com.lhz.framework.aspect.log.annotation.SysLog;
import com.lhz.framework.security.annotation.CheckPermission;
import com.lhz.project.system.model.entity.task.Task;
import com.lhz.project.system.model.param.task.TaskParam;
import com.lhz.project.system.model.vo.task.TaskVo;
import com.lhz.project.system.service.task.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/2/29 15:42
 * @Description: 定时任务接口
 **/
@Tag(name = "任务管理")
@RestController
@RequestMapping("task")
@ApiSort(45)
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 查询任务列表
     *
     * @return
     */
    @Operation(summary = "任务列表", description = "任务列表")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Task.class))))
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    @SysLog(model = "任务管理", name = "任务列表")
    @CheckPermission(code = "system:task:query")
    public ResponseObject taskList() {
        return ResponseObject.success(taskService.taskList());
    }

    /**
     * 新增任务
     *
     * @param param
     */
    @Operation(summary = "新增任务", description = "新增任务")
    @ApiOperationSupport(order = 10)
    @PostMapping("/add")
    @SysLog(model = "任务管理", name = "新增任务")
    @CheckPermission(code = "system:task:add")
    // 方式一：通过BindingResult bindingResult进行如AOP
    public ResponseObject addTask(@Valid @RequestBody TaskParam param) {
        taskService.addTask(param);
        return ResponseObject.success();
    }

    //更新任务
    @Operation(summary = "更新任务", description = "更新任务")
    @ApiOperationSupport(order = 15)
    @PutMapping("/update")
    @SysLog(model = "任务管理", name = "更新任务")
    @CheckPermission(code = "system:task:update")
    // 方式一：使用GlobalException处理
    public ResponseObject updateTask(@Valid @RequestBody TaskParam param) {
        taskService.updateTask(param);
        return ResponseObject.success();
    }

    //删除任务
    @Operation(summary = "删除任务", description = "删除任务")
    @ApiOperationSupport(order = 20)
    @Parameters({
            @Parameter(name = "id", description = "任务id", in = ParameterIn.PATH, required = true, schema = @Schema(type = "String")),
    })
    @DeleteMapping("delete/{id}")
    @SysLog(model = "任务管理", name = "删除任务")
    @CheckPermission(code = "system:task:delete")
    public ResponseObject deleteTask(@PathVariable("id") String id) {
        taskService.deleteTask(id);
        return ResponseObject.success();
    }

    //暂停任务
    @Operation(summary = "暂停任务", description = "暂停任务")
    @ApiOperationSupport(order = 25)
    @Parameters({
            @Parameter(name = "id", description = "任务id", in = ParameterIn.PATH, required = true, schema = @Schema(type = "String")),
    })
    @PostMapping("stop/{id}")
    @SysLog(model = "任务管理", name = "暂停任务")
    @CheckPermission(code = "system:task:stop")
    public ResponseObject stopTask(@PathVariable("id") String id) {
        taskService.stopTask(id);
        return ResponseObject.success();
    }

    //执行任务
    @Operation(summary = "执行任务", description = "执行任务")
    @ApiOperationSupport(order = 30)
    @Parameters({
            @Parameter(name = "id", description = "任务id", in = ParameterIn.PATH, required = true, schema = @Schema(type = "String")),
    })
    @PostMapping("invoke/{id}")
    @SysLog(model = "任务管理", name = "执行任务")
    @CheckPermission(code = "system:task:invoke")
    public ResponseObject invokeTask(@PathVariable("id") String id) {
        taskService.invokeTask(id);
        return ResponseObject.success();
    }

    //查询详情
    @Operation(summary = "查询详情", description = "查询详情")
    @ApiResponse(content = @Content(schema = @Schema(implementation = TaskVo.class)))
    @ApiOperationSupport(order = 35)
    @Parameters({
            @Parameter(name = "id", description = "任务id", in = ParameterIn.PATH, required = true, schema = @Schema(type = "String")),
    })
    @SysLog(model = "任务管理", name = "查询详情")
    @CheckPermission(code = "system:task:query")
    @GetMapping("info/{id}")
    public ResponseObject getTaskById(@PathVariable("id") String id) {
        return ResponseObject.success(taskService.getTaskById(id));
    }

}
